﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using System.IO;

namespace uploader_windows_form
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void UploadButton_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ShowDialog();
            if (this.openFileDialog1.FileNames!=null)
            {
                string[] filePaths = openFileDialog1.FileNames;
                try
                {
                    this.status_ProgressBar1.Value = 0;
                    this.status_ProgressBar1.Visible = true;
                    this.status_ProgressBar1.Maximum = filePaths.Count();
                    foreach (string filename in filePaths)
                    {
                        this.status_ProgressBar1.Value += 1;
                        Application.DoEvents();
                        using (FileStream fs = new FileStream(filename, FileMode.Open))
                        {
                            //connect to SharePoint Context
                            using (ClientContext CNX2 = new ClientContext(ServerTextBox.Text))
                            {
                                //Check Credential Type & Set Credentials typed
                                if (DifferentCredentialRadio.Checked == true)
                                {
                                    CNX2.Credentials = new System.Net.NetworkCredential(UsertextBox.Text, PasswordTextBox.Text, DomainTextBox.Text);
                                }
                                else
                                {
                                    CNX2.AuthenticationMode = ClientAuthenticationMode.Default;
                                }
                                Microsoft.SharePoint.Client.File.SaveBinaryDirect(CNX2, "/" + this.LibrariesTreeView.SelectedNode.Text + "/" + System.IO.Path.GetFileName(filename), fs, false);
                            }
                        }
                    }
                    this.status_ProgressBar1.Visible = false;
                    this.Status_bar_lable.Text = filePaths.Count().ToString() + " files uploade successfuly";
                }
                catch (Exception ex)
                {
                    this.Status_bar_lable.Text = ex.Message;
                    this.status_ProgressBar1.Visible = false;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //enable Upload Button after make sure the Document List selected
            if (e.Node.IsSelected)
            {
                this.UploadButton.Enabled = true;
            }
        }
        private void ConnectButton_Click(object sender, EventArgs e)
        {
            //Disable Connect Button to disallow any error for connection
            this.ConnectButton.Enabled = false;
            this.DisconnectButton.Enabled = true;

            //connect to SharePoint Context
            try
            {
                using (ClientContext CNX = new ClientContext(ServerTextBox.Text))
                {
                    //Check Credential Type & Set Credentials typed
                    if (DifferentCredentialRadio.Checked == true)
                    {
                        CNX.Credentials = new System.Net.NetworkCredential(UsertextBox.Text, PasswordTextBox.Text, DomainTextBox.Text);
                    }
                    else
                    {
                        CNX.AuthenticationMode = ClientAuthenticationMode.Default;
                    }

                    //Load Libraries from SharePoint
                    CNX.Load(CNX.Web.Lists);
                    CNX.ExecuteQuery();
                    this.Status_bar_lable.Text = "Documents Loaded";
                    //Get only Document Library Lists
                    this.LibrariesTreeView.Nodes.Clear();
                    foreach (List list in CNX.Web.Lists)
                        try
                        {
                            if (list.BaseType.ToString() == "DocumentLibrary" && !list.IsApplicationList && !list.Hidden)
                            {
                                this.LibrariesTreeView.Nodes.Add(list.Title);
                            }
                            this.SPDocumentGroupBox.Enabled = true;
                        }
                        catch (Exception ex)
                        {
                            this.Status_bar_lable.Text = ex.Message;
                        }

                }
            }
            catch (Exception ex)
            {
                this.ConnectButton.Enabled = true;
                this.DisconnectButton.Enabled = false;
                this.Status_bar_lable.Text = ex.Message;
            }
        }
        private void DifferentCredentialRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (DifferentCredentialRadio.Checked == true)
            {
                this.CredentialGroupBox.Enabled = true;
            }
            else
            {
                this.CredentialGroupBox.Enabled = false;
            }
        }

        private void DisconnectButton_Click(object sender, EventArgs e)
        {
            this.ConnectButton.Enabled = true;
            this.DisconnectButton.Enabled = false;
            this.LibrariesTreeView.Nodes.Clear();
            SPDocumentGroupBox.Enabled = false;
            this.Status_bar_lable.Text = "Disconnected!";
        }
    }
}
